﻿

Public Class 订单记录

    Dim SeachCode As String         '输入的单号

    Dim ZT_title As Font = New Font(New FontFamily("黑体"), 18)         '标题字体
    Dim ZT_Littl As Font = New Font(New FontFamily("宋体"), 12)         '页眉字体

    Dim ZT_phone As Font = New Font(New FontFamily("幼圆"), 10)         '页脚字体1
    Dim ZT_litt2 As Font = New Font(New FontFamily("幼圆"), 8)          '页脚字体2
    Dim ZT_titl2 As Font = New Font(New FontFamily("幼圆"), 15)         '标题字体2



    '关闭窗体
    Private Sub 订单记录_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SelsRsForm = Nothing
    End Sub

    '窗体加载
    Private Sub 订单记录_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub 订单记录_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        '加载今天单号
        LoadDH_today()



        '控件初始化
        Label4.Text = ""
        Label5.Text = ""
        Label6.Text = ""
        Label7.Text = ""

        TextBox1.Enabled = True
        ComboBox1.Enabled = False
        ComboBox2.Enabled = False
        DateTimePicker1.Enabled = False

        RadioButton1.Checked = True


    End Sub
    '加载今天单号
    Private Sub LoadDH_today()
        Dim da As String = Now.Day.ToString
        If Now.Day < 10 Then da = "0" + da

        Dim mh As String = Now.Month.ToString
        If Now.Month < 10 Then mh = "0" + mh

        Dim dh_head As String = Now.Year.ToString + mh + da
        Dim sqltemp As String = "select 单号 from 单号表 where 单号头 ='" + dh_head + "'"
        Dim ds_1 As DataSet = SQLtoDataSet(sqltemp)
        If ds_1.Tables(0).Rows.Count > 0 Then
            ComboBox1.Items.Clear()
            For i = 0 To ds_1.Tables(0).Rows.Count - 1
                ComboBox1.Items.Add(ds_1.Tables(0).Rows(i).Item(0).ToString)
            Next
        Else
            ComboBox1.Items.Clear()
            ComboBox1.Items.Add("今天未有售单")
            ComboBox1.SelectedIndex = 0
        End If

    End Sub

    '选择订单项
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked Then
            TextBox1.Enabled = True
        Else
            TextBox1.Enabled = False
        End If
    End Sub
    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked Then
            LoadDH_today()
            ComboBox1.Enabled = True
        Else
            ComboBox1.Enabled = False
        End If
    End Sub
    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        If RadioButton3.Checked Then
            DateTimePicker1_ValueChanged(sender, e)
            DateTimePicker1.Enabled = True
            ComboBox2.Enabled = True
        Else
            ComboBox2.Enabled = False
            DateTimePicker1.Enabled = False
        End If
    End Sub

    '选择指定日期
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim da As String = DateTimePicker1.Value.Day.ToString
        If DateTimePicker1.Value.Day < 10 Then da = "0" + da

        Dim mh As String = DateTimePicker1.Value.Month.ToString
        If DateTimePicker1.Value.Month < 10 Then mh = "0" + mh

        Dim dh_head As String = DateTimePicker1.Value.Year.ToString + mh + da
        Dim sqltemp As String = "select 单号 from 单号表 where 单号头 ='" + dh_head + "'"
        Dim ds_1 As DataSet = SQLtoDataSet(sqltemp)
        If ds_1.Tables(0).Rows.Count > 0 Then
            ComboBox2.Items.Clear()
            For i = 0 To ds_1.Tables(0).Rows.Count - 1
                ComboBox2.Items.Add(ds_1.Tables(0).Rows(i).Item(0).ToString)
            Next
        Else
            ComboBox2.Items.Clear()
            ComboBox2.Items.Add("该天未有售单")
            ComboBox2.SelectedIndex = 0
        End If
    End Sub

    '查找按扭
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If RadioButton1.Checked Then
            If TextBox1.Text = "" Then MsgBox("单号不能为空，请输入单号") : TextBox1.Focus() : Exit Sub
            SeachCode = TextBox1.Text
            LoadCode()

        ElseIf RadioButton2.Checked Then
            If ComboBox1.Text = "" Or ComboBox1.Text = "今天未有售单" Then MsgBox("未选择单号，请选择单号") : ComboBox1.Focus() : Exit Sub
            SeachCode = ComboBox1.Text
            LoadCode()

        ElseIf RadioButton3.Checked Then
            If ComboBox2.Text = "" Or ComboBox2.Text = "该天未有售单" Then MsgBox("未选择单号，请选择单号") : ComboBox2.Focus() : Exit Sub
            SeachCode = ComboBox2.Text
            LoadCode()
        End If

    End Sub
    '搜索单号
    Private Sub LoadCode()

        Dim sqltem As String = "select * from 出售表 where 单号 ='" + SeachCode + "'"
        Dim dsa As DataSet = SQLtoDataSet(sqltem)
        Dim dsr As DataRowCollection = dsa.Tables(0).Rows
        If dsr.Count > 0 Then
            Label4.Text = dsr(0)("购机客户").ToString
            Label5.Text = dsr(0)("联系电话").ToString
            Label6.Text = dsr(0)("地址").ToString
            Label7.Text = dsr(0)("日期").ToString

            DataGridView1.Rows.Clear()
            For i = 0 To dsr.Count - 1
                Dim dar As DataRow = dsr.Item(i)
                Dim row1() As String = {dar("品牌").ToString, dar("型号").ToString, dar("售出单价").ToString, dar("售出数量").ToString, dar("出售总额").ToString, dar("备注1").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        Else
            Label4.Text = ""
            Label5.Text = ""
            Label6.Text = ""
            Label7.Text = ""

            MsgBox("没有找到你要的售单！")
            DataGridView1.Rows.Clear()
        End If

    End Sub

    '打印按扭
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If DataGridView1.Rows.Count = 0 Then
            MsgBox("没有记录可进行打印操作")
            Exit Sub
        End If

        '强制横向打印

        '预览

        CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
        PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize(Printing.PaperKind.Custom, 815, 510)

        PrintPreviewDialog1.ShowDialog()


    End Sub
    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        '页眉
        g.DrawLine(Pens.Black, 40, 40, 810 - 50, 40)
        g.DrawString("买得放心  用得开心", ZT_litt2, Brushes.Black, 40, 23)
        g.DrawString("绍康电器  信心的保证", ZT_litt2, Brushes.Black, 693 - 50, 23)

        '标题：原设置常化设置，现改出这里的读写设置
        Dim TiS As TitleSet = ReadTx("SEL.txt")
        g.DrawString(TiS.T1, ZT_title, Brushes.Black, TiS.T1x, TiS.T1y)
        g.DrawString(TiS.T2, ZT_title, Brushes.Black, TiS.T2x, TiS.T2y)
        g.DrawString(TiS.T3, ZT_title, Brushes.Black, TiS.T3x, TiS.T3y)

        '标题：原设置常化设置，现改出上面的读写设置
        'g.DrawString("东 莞 市 虎 门 伟 康 冷 冻 设 备", ZT_title, Brushes.Black, 213 - 50, 50)
        'g.DrawString("东 莞 市 虎 门 绍 康 电 器 商 店", ZT_title, Brushes.Black, 213 - 50, 95)
        'g.DrawString("商 品 销 售 单", ZT_title, Brushes.Black, 325 - 50 + 3, 135)

        '电话、电址
        g.DrawString("电话：13450000055 13902613906 85512003", ZT_phone, Brushes.Black, 220, 510 - 50)
        g.DrawString("地址：虎门执信路43号", ZT_phone, Brushes.Black, 40, 510 - 50)

        '打印日期、出单日期
        g.DrawString("打印时间：" + Now.ToString, ZT_litt2, Brushes.Black, 650 - 50, 510 - 50)
        g.DrawString("日期：" + Label7.Text, ZT_phone, Brushes.Black, 640 - 50, 185)

        '客户、地址
        g.DrawString(Label4.Text, ZT_titl2, Brushes.Black, 40, 155)
        g.DrawString("联系：" + Label5.Text, ZT_phone, Brushes.Black, 42, 185)
        g.DrawString(Label6.Text, ZT_phone, Brushes.Black, 220, 185)

        '单号：
        g.DrawString("N0." + SeachCode, ZT_titl2, Brushes.Black, 640 - 50, 157)

        '表格
        DrawTable(g, pen, rect)

        '表头
        g.DrawString("品 名 规 格", ZT_phone, Brushes.Black, 100 - 25, 220)
        g.DrawString("单 价", ZT_phone, Brushes.Black, 340 - 50, 220)
        g.DrawString("数 量", ZT_phone, Brushes.Black, 467 - 50, 220)
        g.DrawString("小 计", ZT_phone, Brushes.Black, 594 - 50, 220)
        g.DrawString("备 注", ZT_phone, Brushes.Black, 721 - 50, 220)

        '填表
        DrawList(g, pen, rect)
    End Sub
    '画表
    Private Sub DrawTable(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        '竖边
        g.DrawLine(Pens.Black, 41, 217, 41, 496 - 60)
        g.DrawLine(Pens.Black, 809 - 50, 217, 809 - 50, 496 - 60)

        '横边
        g.DrawLine(Pens.Black, 40, 213, 810 - 50, 213)
        g.DrawLine(Pens.Black, 48, 241, 802 - 50, 241)
        g.DrawLine(Pens.Black, 40, 500 - 60, 810 - 50, 500 - 60)
        g.DrawLine(Pens.Black, 48, 451 - 60, 802 - 50, 451 - 60)

        '内横
        For i = 1 To 6 - 2
            g.DrawLine(Pens.Black, 48, 241 + (30 * i), 802 - 50, 241 + (30 * i))
        Next

        '内竖--中线
        g.DrawLine(Pens.Black, 425 - 50, 217, 425 - 50, 237)
        g.DrawLine(Pens.Black, 425 - 50, 245, 425 - 50, 447 - 60)

        '内竖--后三分
        g.DrawLine(Pens.Black, 553 - 50, 217, 553 - 50, 237)
        g.DrawLine(Pens.Black, 553 - 50, 245, 553 - 50, 447 - 60)
        g.DrawLine(Pens.Black, 553 - 50, 455 - 60, 553 - 50, 496 - 60)

        g.DrawLine(Pens.Black, 681 - 50, 217, 681 - 50, 237)
        g.DrawLine(Pens.Black, 681 - 50, 245, 681 - 50, 447 - 60)
        g.DrawLine(Pens.Black, 681 - 50, 455 - 60, 681 - 50, 496 - 60)


        '内竖--前三份显一
        g.DrawLine(Pens.Black, 296 - 50, 217, 296 - 50, 237)
        g.DrawLine(Pens.Black, 296 - 50, 245, 296 - 50, 447 - 60)

        g.DrawLine(Pens.Black, 168 - 50, 455 - 60, 168 - 50, 496 - 60)

    End Sub
    '填表
    Private Sub DrawList(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        Dim acount As Integer = DataGridView1.Rows.Count
        Dim ZE As Integer = 0
        Dim i As Integer
        For i = 0 To acount - 1
            g.DrawString(DataGridView1.Rows(i).Cells("品牌").Value.ToString + "  " + DataGridView1.Rows(i).Cells("型号").Value.ToString, ZT_phone, Brushes.Black, 100 - 25, 220 + (i + 1) * 30)
            g.DrawString("￥" + DataGridView1.Rows(i).Cells("售价").Value.ToString, ZT_phone, Brushes.Black, 340 - 50, 220 + (i + 1) * 30)
            g.DrawString(DataGridView1.Rows(i).Cells("数量").Value.ToString + "台", ZT_phone, Brushes.Black, 467 - 50, 220 + (i + 1) * 30)
            g.DrawString("￥" + DataGridView1.Rows(i).Cells("小计").Value.ToString, ZT_phone, Brushes.Black, 594 - 50, 220 + (i + 1) * 30)
            g.DrawString(DataGridView1.Rows(i).Cells("备注").Value.ToString, ZT_titl2, Brushes.Black, 640, 220 + (i + 1) * 30)
            ZE += DataGridView1.Rows(i).Cells("小计").Value
        Next
        Dim bcount As Integer = 7 - 2 - acount
        While bcount > 0
            g.DrawString("/", ZT_phone, Brushes.Black, 100 - 25, 220 + (i + 1) * 30)
            g.DrawString("/", ZT_phone, Brushes.Black, 340 - 50, 220 + (i + 1) * 30)
            g.DrawString("/", ZT_phone, Brushes.Black, 467 - 50, 220 + (i + 1) * 30)
            g.DrawString("/", ZT_phone, Brushes.Black, 594 - 50, 220 + (i + 1) * 30)
            i += 1
            bcount -= 1
        End While

        '总计
        g.DrawString("￥" + ZE.ToString, ZT_titl2, Brushes.Black, 580 - 50, 467 - 60)

        g.DrawString("合 计", ZT_phone, Brushes.Black, 55, 470 - 60)
        g.DrawString("大写：" + RMBtoBig(ZE), ZT_phone, Brushes.Black, 180 - 50, 470 - 60)


        g.DrawString("经办人：" + AccountName, ZT_phone, Brushes.Black, 690 - 50, 470 - 60)

    End Sub

    '删除按扭
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If DataGridView1.Rows.Count = 0 Then MsgBox("目录未选择有内容的售单！") : Exit Sub


        Dim a As MsgBoxResult = MsgBox("你确定要删除信单'" + SeachCode + "'及其内容？", MsgBoxStyle.YesNo, "警告")
        If a = MsgBoxResult.No Then Exit Sub

        Dim sqltem1 As String = "delete from 出售表 where 单号='" + SeachCode + "'"
        Dim sqltem2 As String = "delete from 单号表 where 单号='" + SeachCode + "'"
        SQLupdata(sqltem1)
        SQLupdata(sqltem2)

        DataGridView1.Rows.Clear()
        Label4.Text = ""
        Label5.Text = ""
        Label6.Text = ""
        Label7.Text = ""

        RadioButton1.Checked = True


    End Sub

    '选择单号改变时
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        DataGridView1.Rows.Clear()
        Label4.Text = ""
        Label5.Text = ""
        Label6.Text = ""
        Label7.Text = ""
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        DataGridView1.Rows.Clear()
        Label4.Text = ""
        Label5.Text = ""
        Label6.Text = ""
        Label7.Text = ""
    End Sub

End Class